<template>
  <div>
    <Card
      size="small"
      :title="echartsData.title"
      :bordered="false"
      class="!mt-0 !rounded-lg !p-6px"
    >
      <div class="flex flex-row items-center justify-center">
        <RangePicker show-time v-model:value="selectTime" placement="bottomLeft" />
      </div>
      <div ref="chartRef" class="h-160px w-full mt-1"></div>
    </Card>
  </div>
</template>

<script setup lang="ts">
  import { Card, RangePicker } from 'ant-design-vue';
  import { useECharts } from '/@/hooks/web/useECharts';
  import { ref, Ref, watch } from 'vue';
  import * as echarts from 'echarts';
  import dayjs from 'dayjs';

  const selectTime = ref<any>([dayjs(), dayjs().subtract(1, 'hour')]);
  let emits = defineEmits(['selectTime']);
  let props = defineProps(['echartsData']);
  const chartRef = ref<HTMLDivElement | null>(null);
  const { setOptions } = useECharts(chartRef as Ref<HTMLDivElement>);
  watch(
    () => props.echartsData.echartsList,
    (nVal) => {
      let seriesArr: any = [];
      let colorArr = ['0, 62, 246', '0, 193, 142', '253, 148, 67'];
      nVal.forEach((item: any, index) => {
        seriesArr.push({
          name: item.title,
          type: 'line',
          symbolSize: 1,
          symbol: 'circle',
          data: item.data,
          areaStyle: {
            normal: {
              color: new echarts.graphic.LinearGradient(
                0,
                0,
                0,
                1,
                [
                  {
                    offset: 0,
                    color: `rgba(${colorArr[index]},.2)`,
                  },
                  {
                    offset: 1,
                    color: 'rgba(255, 255, 255,0)',
                  },
                ],
                false,
              ),
            },
          },
          itemStyle: {
            normal: {
              color: `rgb(${colorArr[index]})`,
            },
          },
          lineStyle: {
            normal: {},
          },
        });
      });
      setOptions({
        backgroundColor: '#fff',
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            lineStyle: {
              color: '#ddd',
            },
          },
          backgroundColor: 'rgba(255,255,255,1)',
          padding: [5, 10],
          textStyle: {
            color: '#000',
          },
        },
        legend: {
          right: 'center',
          top: '0%',
          width: '100%',
          textStyle: {
            color: '#000',
            fontSize: 10,
          },
          data: nVal.map((val: any) => {
            return val.title;
          }),
        },
        grid: {
          left: '0%',
          right: '0%',
          bottom: '0%',
          top: '18%',
          containLabel: true,
        },
        xAxis: {
          type: 'time',
          splitLine: {
            show: true,
            interval: 'auto',
            lineStyle: {
              type: 'dashed',
              color: ['#cfcfcf'],
            },
          },
          axisTick: {
            show: false,
          },
          axisLine: {
            lineStyle: {
              color: '#cfcfcf',
            },
          },
          axisLabel: {
            fontSize: 7,
            color: '#9e9d9f',
          },
        },
        yAxis: [
          {
            name: '(%)',
            type: 'value',
            splitLine: {
              show: true,
              lineStyle: {
                type: 'dashed',
                color: ['#cfcfcf'],
              },
            },
            axisTick: {
              show: false,
            },
            axisLine: {
              show: true,
              lineStyle: {
                color: '#cfcfcf',
              },
            },
            axisLabel: {
              fontSize: 8,
              color: '#9e9d9f',
            },
          },
        ],
        series: seriesArr,
      });
    },
    {
      deep: true,
    },
  );
  watch(
    () => selectTime.value,
    (nVal) => {
      emits('selectTime', [dayjs(nVal[0]).valueOf(), dayjs(nVal[1]).valueOf()]);
    },
  );
</script>
